package com.gitee.chili.leisure.api.currency;

import com.gitee.chili.leisure.ResponseResult;
import com.gitee.chili.leisure.annotation.PathRestController;
import com.gitee.chili.leisure.domain.Currency;
import com.gitee.chili.leisure.service.ICurrencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * CurrencyController
 *
 * @author week solider
 * @since 2020.09.08
 */
@Validated
@PathRestController("/currency")
public class CurrencyController {

    @Autowired
    private ICurrencyService currencyService;


    @PostMapping("/query")
    public ResponseResult<Currency> query(Long currencyId) {

        if (Objects.isNull(currencyId) || currencyId < 1) {
            return ResponseResult.failure("未知币种信息");
        }

        Optional<Currency> optional = currencyService.findById(currencyId);
        if (optional.isPresent()) {
            return ResponseResult.success(optional.get());
        }

        return ResponseResult.failure("查询失败");
    }

    @PostMapping("/all")
    public ResponseResult<List<Currency>> all() {
        List<Currency> all = currencyService.findAll();
        return ResponseResult.success(all);
    }
}
